package com.pactera.madp.cp.controller.sidebar.employee;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.common.log.annotation.SysLog;
import com.pactera.madp.cp.api.dto.BlacklistDTO;
import com.pactera.madp.cp.api.entity.WorkContactEntity;
import com.pactera.madp.cp.api.entity.WorkCorpBlacklistEntity;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.mapper.WorkContactMapper;
import com.pactera.madp.cp.service.IWorkCorpBlacklistService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping("/workEmployeeOperate")
@Api(value = "workEmployeeOperate", tags = "成员操作")
public class EmployeeOperateController {

    @Autowired
    private WorkContactMapper contactMapper;
    @Autowired
    private IWorkCorpBlacklistService workCorpBlacklistService;

    @SysLog("添加成员到黑名单")
    @ApiOperation("添加成员到黑名单")
    @PostMapping("/BlacklistSave")
    public R BlacklistSave(@RequestBody BlacklistDTO dto) {
        Integer corpId = CorpContextHolder.getCorpId();
        return workCorpBlacklistService.save(corpId, dto);
    }

    @SysLog("移除黑名单")
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("移除黑名单")
    @GetMapping("/BlacklistRemove")
    public R BlacklistRemove(Integer id) {
        QueryWrapper<WorkCorpBlacklistEntity> wrapper = new QueryWrapper<WorkCorpBlacklistEntity>()
                .eq("id", id);

        if (workCorpBlacklistService.count(wrapper) == 0) {
            return R.ok("该客户已不再黑名单");
        }
        Integer contactId = workCorpBlacklistService.getOne(wrapper).getContactId();
        UpdateWrapper<WorkContactEntity> wrapper1 = new UpdateWrapper<WorkContactEntity>();
        wrapper1.eq("id", contactId);
        wrapper1.set("in_blacklist", 0);
        int update = contactMapper.update(null, wrapper1);
        return update > 0?R.ok("移除成功") : R.failed("移除失败");
    }

    @ApiOperation("黑名单列表查询")
    @GetMapping("/BlacklistQueryByDTO")
    public R BlacklistQuery(BlacklistDTO dto, Page page) {

        return R.ok(workCorpBlacklistService.query(dto, page));
    }

    @ApiOperation("根据客户id查询黑名单客户")
    @GetMapping("/selectByContactId")
    public R selectByContactId(Integer contactId) {
        QueryWrapper<WorkCorpBlacklistEntity> wrapper = new QueryWrapper<WorkCorpBlacklistEntity>()
                .select("id, contact_name, employee_name, create_time, reason, contact_type, unionid")
                .eq("contact_id", contactId)
                .eq("corp_id", CorpContextHolder.getCorpId());
        List<WorkCorpBlacklistEntity> list = workCorpBlacklistService.list(wrapper);
        if (list.size() == 0) {
            return R.failed("该客户不在黑名单");
        } else if (list.size() == 1) {
            return R.ok(list.get(0));
        } else {
            return R.failed("数据库有问题");
        }
    }

}
